<!-- 文档查询结果列表 -->
<template>
	<view>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-empty description="暂无数据" v-show="emptyData"/>
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="load"
                offset="20" :error.sync="error" error-text="请求失败，点击重新加载" :immediate-check="immediateCheck">
        <van-checkbox-group v-model="result">
          <van-swipe-cell  v-for="(item,index) in list" :right-width="90">
            <van-cell center :title="item.title" :label="'公文类型：'+item.name" :value="item.createTime"
                      @click="toDetail(item.proInstId)" value-class="text-sm">
            </van-cell>
            <template #right>
              <van-checkbox :name="item.proInstId" :key="item.proInstId" @click.native="collectionChange(item,index)">
                <text v-if="isChecked(item.proInstId)">{{ collectionState }}</text>
                <text v v-else>{{ inCollectionState }}</text>
                <template #icon="props">
                  <img class="img-icon" :src="isChecked(item.proInstId) ? activeIcon : inactiveIcon" />
                </template>
              </van-checkbox>
            </template>
          </van-swipe-cell>
        </van-checkbox-group>
      </van-list>
    </van-pull-refresh>
	</view>
</template>

<script>
	export default {
    props: {
      queryData: {
        type: Object,
        default: {}
      },
    },
		data() {
			return {
        //收藏
        result:[],
        collectionState:'已收藏',
        inCollectionState:'收藏',
        activeIcon: require('@/static/imgs/icon/icon-collection-active.png'),
        inactiveIcon: require('@/static/imgs/icon/icon-collection.png'),
			  searchData:{
            title: '',//标题
            bizTypeCode: '',//公文类型code
            bizTypeName:'全部',//公文类型名称
            cbr: '',//呈报人code
            cbrName:'',//呈报人name
            cbdept: '',//呈报单位code
            cbdeptName: '',//呈报单位name
            personalClassification:'',//个性分类code
            personalClassificationName:'',//个性分类name
            taskNumber:'',//外事任务编号
            date: '',
            createTimeStart:'',
            createTimeEnd:'',
            orderName:'completeTime',//排序completeTime结束时间,createTime提交时间,code公文类型,cbdept单位
            orderNameName:'结束时间'
        },
        emptyData:false,//是否显示暂无数据图片
        list: [],
        error: false,     // 错误状态
        loading: false,   // 加载状态
        finished: false,  // 是否加载
        refreshing: false,// 是否刷新
        page: 1,   		// 页数
        rows: 10, 		// 条数
        immediateCheck: true, // 是否在初始化时立即执行滚动位置检查　　　
			}
		},
    watch: {
      queryData:{
        handler(nVal, oVal) {
          if(this.util.isNotEmpty(nVal)){
            this.searchData = nVal
            this.onRefresh()
          }
        },
        immediate: true,
        deep: true
      }
    },
		methods: {
      // 判断是否选中
      isChecked(proInstId) {
        let coll = this.result.filter(t => t == proInstId)
        if(coll != null && coll.length > 0){
          return true
        } else {
          return false
        }
      },

      //收藏状态
      collectionChange(item, index){
        let flag = this.isChecked(item.proInstId)
        if(flag){
          // console.log('取消收藏')
          // 取消收藏
          this.cancelcCollection(item,index)
        }else{
          // console.log('收藏')
          // this.result.push(item.bizId)
          // 调用收藏接口
          this.addCollection(item,index)
        }

      },
      //添加收藏
      addCollection(item,index){
        this.$http({
          url: this.$http.adornUrl(`/caac/oa/common/collection/addcollection`),
          method: 'get',
          params: this.$http.adornParams({
            'systemKey': this.cache.get('userInfo').systemKey,
            'systemFlag': this.cache.get('userInfo').systemFlag,
            'gwId':item.proInstId,
            'dirId':'root',
            'gwTitle':item.title,
            'gwType':item.code,
            'bizId':item.bizId
          })
        }).then(({data}) => {
          //{"code":200,"data":{"message":"收藏成功！"},"message":null}
          if(data.code == 200){
            this.$toast('收藏成功')
          }else{
            this.$toast(data.data.message)
          }
        })
      },
      //取消收藏
      cancelcCollection(item,index){
        this.$http({
          url: this.$http.adornUrl(`/caac/oa/common/collection/cancelcollection`),
          method: 'delete',
          params: this.$http.adornParams({
            'systemKey': this.cache.get('userInfo').systemKey,
            'systemFlag': this.cache.get('userInfo').systemFlag,
            'gwId':item.proInstId,
          })
        }).then(({data}) => {
          //{"code":200,"data":"取消收藏成功!","message":null}
          if(data.code == 200){
            this.$toast('取消收藏成功')
          }else{
            this.$toast(data.data.message)
          }
        })
      },
      // 下拉刷新
      onRefresh() {
        // 先清空数据
        this.list = []
        // 将没有更多的状态改成false
        this.finished = false
        // 默认加载第一页
        this.page = 1
        this.queryList()
        this.refreshing = false
      },
			load() {
        // 页数设置
        if(this.list.length === 0){
          this.page = 1
        } else {
          this.page++
        }
        // 将下拉刷新停止
        this.refreshing = false;
        setTimeout(() => {
          this.queryList()
        }, 500)
			},

        //查询公文
      queryList(){
        this.loading = true
        // 获取要显示的按钮
        this.$http({
          url: this.$http.adornUrl(`/caac/oa/mywork/gwwork/gwsearch`),
          method: 'get',
          params: this.$http.adornParams({
            'systemKey': this.cache.get('userInfo').systemKey,//固定值
            'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
            'title': this.searchData.title,
            'bizTypeCode':this.searchData.bizTypeCode,
            'createTimeStart':this.searchData.createTimeStart,
            'createTimeEnd':this.searchData.createTimeEnd,
            'personalClassification':this.searchData.personalClassification,
            'taskNumber':this.searchData.taskNumber,
            'orderName': this.searchData.orderName,
            'orgType':'',
            'cbr':this.searchData.cbr,
            'cbdept':this.searchData.cbdept,
            'page':this.page,
            'rows':this.rows
          })
        }).then(({data}) => {
          console.log("待收来电：data: " + JSON.stringify(data));
          if(data.code==200){
            if(this.util.isNotEmpty(data.data)) {
              this.loading = false;
              if(this.page == 1) {
                this.list = data.data.rows
              } else {
                data.data.rows.forEach( t => this.list.push(t))
              }
              //收藏
              this.result=[]
              this.list.forEach((item)=>{
                if(item.isCollect == '1'){
                  this.result.push(item.proInstId)
                }
              })

              if(this.list.length==0){
                this.emptyData = true
              }else{
                this.emptyData = false
              }
              // 判断当前页是否为最后一页
              if((this.page*this.rows) > data.data.total) {
                this.finished = true
              }
            } else {
              this.finished = true
            }
          }else{
            this.error = true
            console.log("出错data: " + JSON.stringify(data));
          }
        }).catch((err) => {
          this.error = true
        }).finally(() => {
          // 关闭加载状态
          this.loading = false
        })
      },

      //跳转详情code:发文类型，proInstId
      toDetail(proInstId){
        var from = "search"//如果是从查询里进详情，则不显示操作按钮
        // 会签件--hqj，发文（函）--glj_fw，呈批件--glj_cpj，发电--fd_hbglj，发电抄阅--hbgljfd，收文--glj_sw
        var _this = this
        this.$http({
          url: this.$http.adornUrl(`/caac/oa/comm/comm/openwfneededdata`),
          method: 'get',
          params: this.$http.adornParams({
            'proInstId': proInstId,//发文详情返回的proInstId
            'systemKey': this.cache.get('userInfo').systemKey,//固定值
            'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
          })
        }).then(({data}) => {
          if(data.code==200){
            _this.configCode = data.data.configCode
            console.log("公文类型==>"+_this.configCode)
            if(_this.configCode != ''){
              this.common.toAdmin(_this.configCode,proInstId,'search')
            }
          }else{
            this.$toast(data.message)
          }

        })

        // this.openwfneededdata(extendIdForRelate,proInstId)


      },

    }
	}
</script>

<style>
/*收藏样式*/
/deep/  .van-swipe-cell__right{
  display: flex;
  align-items: center;
}
.img-icon {
  height: 40rpx;
}
/*收藏样式*/
</style>
